package com.probox.common.entity.service.customer.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.probox.common.core.enums.DataStatus;
import com.probox.common.core.enums.IsBool;
import com.probox.common.core.enums.ReleaseShopTypeEnum;
import com.probox.common.core.exception.BaseException;
import com.probox.common.core.utils.SecurityUtils;
import com.probox.common.core.utils.StringUtils;
import com.probox.common.core.utils.money.MoneyUtils;
import com.probox.common.entity.domain.customer.CustomerLevelInfo;
import com.probox.common.entity.domain.customer.RechargeInfo;
import com.probox.common.entity.domain.customer.RechargeInfoRights;
import com.probox.common.entity.domain.platfom.PlatformStore;
import com.probox.common.entity.domain.recharge.RechargeRecord;
import com.probox.common.entity.mapper.customer.RechargeInfoMapper;
import com.probox.common.entity.service.customer.ICustomerLevelInfoService;
import com.probox.common.entity.service.customer.ICustomerLevelService;
import com.probox.common.entity.service.customer.IRechargeInfoRightsService;
import com.probox.common.entity.service.customer.IRechargeInfoService;
import com.probox.common.entity.service.platfom.IPlatformStoreService;

import com.probox.common.entity.service.product.IProductInfoService;
import com.probox.common.entity.vo.customer.RechargeRightItem;

import com.probox.customer.api.domain.CustomerLevel;
import com.probox.product.api.domain.ProductInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

import static com.probox.common.core.utils.bean.BeanUtils.beanToMap;

/**
 * 储值信息Service业务层处理
 *
 * @author usoft
 * @date 2022-09-14
 */
@Service
@Transactional
public class RechargeInfoServiceImpl extends ServiceImpl<RechargeInfoMapper, RechargeInfo> implements IRechargeInfoService {
    @Autowired
    private RechargeInfoMapper rechargeInfoMapper;

    @Autowired
    private IPlatformStoreService storeService;

    @Autowired
    private IRechargeInfoRightsService rechargeInfoRightsService;

    @Autowired
    private ICustomerLevelService customerLevelService;


    @Autowired
    private IProductInfoService productInfoService;


    /**
     * 添加储值
     *
     * @param rechargeInfo
     * @return
     */
    @Override
    public boolean addOrEditRecharge(RechargeInfo rechargeInfo) {
//        if (CollectionUtil.isEmpty(rechargeInfo.getRightObjectIdList())) {
//            throw new BaseException("请选择一个权益");
//        }
        rechargeInfo.setRechargeMoney(MoneyUtils.changeY2F2(rechargeInfo.getRechargeMoneyBig()));
        //rechargeInfo.setGoldCoin(rechargeInfo.getGoldCoin() * 100L);
        //rechargeInfo.setSilverCoin(rechargeInfo.getSilverCoin() * 100L);
        boolean result = false;
        if (ObjectUtil.isEmpty(rechargeInfo.getRechargeId())) {
            result = insertRechargeInfo(rechargeInfo);
        } else {
            result = updateRechargeInfo(rechargeInfo);
        }
        if (!result) {
            return result;
        }


        /**
         * 添加储值
         */
        rechargeInfoRightsService.addList(rechargeInfo);
        return result;
    }

    /**
     * 详情
     *
     * @param uid
     * @return
     */
    @Override
    public RechargeInfo getRecharge(Long uid) {
        RechargeInfo rechargeInfo = this.getById(uid);
        if (ObjectUtil.isEmpty(rechargeInfo)) throw new BaseException("储值不存在");
        RechargeInfoRights rechargeInfoRights = new RechargeInfoRights();
        rechargeInfoRights.setRechargeId(uid);
        rechargeInfoRights.setDataStatus(DataStatus.valid.getKey());
        List<RechargeInfoRights> rechargeInfoRightList = rechargeInfoRightsService.queryList(rechargeInfoRights);
        if (CollectionUtil.isNotEmpty(rechargeInfoRightList)) {
            List<HashMap> selectDataMap = new ArrayList<>();
            List<String> typeList = new ArrayList<>();

            rechargeInfoRightList.forEach(right -> {
                if (ReleaseShopTypeEnum.service.getKey().equals(right.getRightType())) {

                } else if (ReleaseShopTypeEnum.card.getKey().equals(right.getRightType())) {

                } else if (ReleaseShopTypeEnum.product.getKey().equals(right.getRightType())) {
                    ProductInfoVo byId = productInfoService.getProductView(right.getRightObjectId());
                    if (ObjectUtil.isNotEmpty(byId)) {
                        HashMap<String, Object> stringObjectHashMap = beanToMap(byId);
                        stringObjectHashMap.put("name", byId.getProductName());
                        stringObjectHashMap.put("type", right.getRightType());
                        selectDataMap.add(stringObjectHashMap);
                        typeList.add(right.getRightType());
                    }
                } else if (ReleaseShopTypeEnum.coupon.getKey().equals(right.getRightType())) {

                }
            });
            rechargeInfo.setSelectDataMap(selectDataMap);
            rechargeInfo.setRightTypeList(typeList);
        }
        return rechargeInfo;
    }


    /**
     * 查询储值信息
     *
     * @param rechargeId 储值信息ID
     * @return 储值信息
     */
    @Override
    public RechargeInfo selectRechargeInfoById(Long rechargeId) {
        return this.getById(rechargeId);
    }

    @Override
    public List<RechargeInfo> selectByStroeId(Long storeId) {
        return rechargeInfoMapper.selectByStoreId(storeId);
    }

    @Override
    public List<RechargeRightItem> getRightByRechargeId(Long rechargeId) {
        return rechargeInfoMapper.getRightByRechargeId(rechargeId);
    }


    @Override
    public List<RechargeInfo> queryList(RechargeInfo rechargeInfo) {
        QueryWrapper<RechargeInfo> queryWrapper = Wrappers.query();
        if (StringUtils.isNotEmpty(String.valueOf(rechargeInfo.getRechargeId())) && rechargeInfo.getRechargeId() != null) {
            queryWrapper.eq("recharge_id", rechargeInfo.getRechargeId());
        }

        if (StringUtils.isNotEmpty(String.valueOf(rechargeInfo.getRechargeName())) && rechargeInfo.getRechargeName() != null) {
            queryWrapper.like("recharge_name", rechargeInfo.getRechargeName());
        }
        if (StringUtils.isNotEmpty(String.valueOf(rechargeInfo.getIsRecommend())) && rechargeInfo.getIsRecommend() != null) {
            queryWrapper.eq("is_recommend", rechargeInfo.getIsRecommend());
        }
        if (StringUtils.isNotEmpty(String.valueOf(rechargeInfo.getRechargeIcon())) && rechargeInfo.getRechargeIcon() != null) {
            queryWrapper.eq("recharge_icon", rechargeInfo.getRechargeIcon());
        }
        if (StringUtils.isNotEmpty(String.valueOf(rechargeInfo.getRechargeSort())) && rechargeInfo.getRechargeSort() != null) {
            queryWrapper.eq("recharge_sort", rechargeInfo.getRechargeSort());
        }
        queryWrapper.ne("data_status", DataStatus.delete.getKey());
        if (StringUtils.isNotEmpty(String.valueOf(rechargeInfo.getStartTime())) && rechargeInfo.getStartTime() != null) {
            queryWrapper.eq("start_time", rechargeInfo.getStartTime());
        }
        if (StringUtils.isNotEmpty(String.valueOf(rechargeInfo.getEndTime())) && rechargeInfo.getEndTime() != null) {
            queryWrapper.eq("end_time", rechargeInfo.getEndTime());
        }
        if (StringUtils.isNotEmpty(String.valueOf(rechargeInfo.getRechargeMoney())) && rechargeInfo.getRechargeMoney() != null) {
            queryWrapper.eq("recharge_money", rechargeInfo.getRechargeMoney());
        }
        if (StringUtils.isNotEmpty(String.valueOf(rechargeInfo.getFirstDouble())) && rechargeInfo.getFirstDouble() != null) {
            queryWrapper.eq("first_double", rechargeInfo.getFirstDouble());
        }
        if (StringUtils.isNotEmpty(String.valueOf(rechargeInfo.getFirstDoubleMonth())) && rechargeInfo.getFirstDoubleMonth() != null) {
            queryWrapper.eq("first_double_month", rechargeInfo.getFirstDoubleMonth());
        }
        if (StringUtils.isNotEmpty(String.valueOf(rechargeInfo.getGoldCoin())) && rechargeInfo.getGoldCoin() != null) {
            queryWrapper.eq("gold_coin", rechargeInfo.getGoldCoin());
        }
        if (StringUtils.isNotEmpty(String.valueOf(rechargeInfo.getSilverCoin())) && rechargeInfo.getSilverCoin() != null) {
            queryWrapper.eq("silver_coin", rechargeInfo.getSilverCoin());
        }
        if (StringUtils.isNotEmpty(String.valueOf(rechargeInfo.getScore())) && rechargeInfo.getScore() != null) {
            queryWrapper.eq("score", rechargeInfo.getScore());
        }
        if (StringUtils.isNotEmpty(String.valueOf(rechargeInfo.getRechargeDesc())) && rechargeInfo.getRechargeDesc() != null) {
            queryWrapper.eq("recharge_desc", rechargeInfo.getRechargeDesc());
        }
        if (StringUtils.isNotEmpty(String.valueOf(rechargeInfo.getDataStatus())) && rechargeInfo.getDataStatus() != null) {
            queryWrapper.eq("data_status", rechargeInfo.getDataStatus());
        }
        if (StringUtils.isNotEmpty(String.valueOf(rechargeInfo.getCreateUser())) && rechargeInfo.getCreateUser() != null) {
            queryWrapper.eq("create_user", rechargeInfo.getCreateUser());
        }
        if (StringUtils.isNotEmpty(String.valueOf(rechargeInfo.getCreateName())) && rechargeInfo.getCreateName() != null) {
            queryWrapper.eq("create_name", rechargeInfo.getCreateName());
        }
        if (StringUtils.isNotEmpty(String.valueOf(rechargeInfo.getUpdateUser())) && rechargeInfo.getUpdateUser() != null) {
            queryWrapper.eq("update_user", rechargeInfo.getUpdateUser());
        }
        if (StringUtils.isNotEmpty(String.valueOf(rechargeInfo.getUpdateName())) && rechargeInfo.getUpdateName() != null) {
            queryWrapper.eq("update_name", rechargeInfo.getUpdateName());
        }
        queryWrapper.orderByDesc("recharge_id");
        return rechargeInfoMapper.selectList(queryWrapper);
    }

    /**
     * 查询储值信息列表
     *
     * @param rechargeInfo 储值信息
     * @return 储值信息
     */
    @Override
    public List<RechargeInfo> getList(RechargeInfo rechargeInfo) {
        QueryWrapper<RechargeInfo> queryWrapper = Wrappers.query();
        if (StringUtils.isNotEmpty(String.valueOf(rechargeInfo.getRechargeId())) && rechargeInfo.getRechargeId() != null) {
            queryWrapper.eq("recharge_id", rechargeInfo.getRechargeId());
        }

        if (StringUtils.isNotEmpty(String.valueOf(rechargeInfo.getRechargeName())) && rechargeInfo.getRechargeName() != null) {
            queryWrapper.like("recharge_name", rechargeInfo.getRechargeName());
        }
        if (StringUtils.isNotEmpty(String.valueOf(rechargeInfo.getIsRecommend())) && rechargeInfo.getIsRecommend() != null) {
            queryWrapper.eq("is_recommend", rechargeInfo.getIsRecommend());
        }
        if (StringUtils.isNotEmpty(String.valueOf(rechargeInfo.getRechargeIcon())) && rechargeInfo.getRechargeIcon() != null) {
            queryWrapper.eq("recharge_icon", rechargeInfo.getRechargeIcon());
        }
        if (StringUtils.isNotEmpty(String.valueOf(rechargeInfo.getRechargeSort())) && rechargeInfo.getRechargeSort() != null) {
            queryWrapper.eq("recharge_sort", rechargeInfo.getRechargeSort());
        }
        queryWrapper.ne("data_status", DataStatus.delete.getKey());
        if (StringUtils.isNotEmpty(String.valueOf(rechargeInfo.getStartTime())) && rechargeInfo.getStartTime() != null) {
            queryWrapper.eq("start_time", rechargeInfo.getStartTime());
        }
        if (StringUtils.isNotEmpty(String.valueOf(rechargeInfo.getEndTime())) && rechargeInfo.getEndTime() != null) {
            queryWrapper.eq("end_time", rechargeInfo.getEndTime());
        }
        if (StringUtils.isNotEmpty(String.valueOf(rechargeInfo.getRechargeMoney())) && rechargeInfo.getRechargeMoney() != null) {
            queryWrapper.eq("recharge_money", rechargeInfo.getRechargeMoney());
        }
        if (StringUtils.isNotEmpty(String.valueOf(rechargeInfo.getFirstDouble())) && rechargeInfo.getFirstDouble() != null) {
            queryWrapper.eq("first_double", rechargeInfo.getFirstDouble());
        }
        if (StringUtils.isNotEmpty(String.valueOf(rechargeInfo.getFirstDoubleMonth())) && rechargeInfo.getFirstDoubleMonth() != null) {
            queryWrapper.eq("first_double_month", rechargeInfo.getFirstDoubleMonth());
        }
        if (StringUtils.isNotEmpty(String.valueOf(rechargeInfo.getGoldCoin())) && rechargeInfo.getGoldCoin() != null) {
            queryWrapper.eq("gold_coin", rechargeInfo.getGoldCoin());
        }
        if (StringUtils.isNotEmpty(String.valueOf(rechargeInfo.getSilverCoin())) && rechargeInfo.getSilverCoin() != null) {
            queryWrapper.eq("silver_coin", rechargeInfo.getSilverCoin());
        }
        if (StringUtils.isNotEmpty(String.valueOf(rechargeInfo.getScore())) && rechargeInfo.getScore() != null) {
            queryWrapper.eq("score", rechargeInfo.getScore());
        }
        if (StringUtils.isNotEmpty(String.valueOf(rechargeInfo.getRechargeDesc())) && rechargeInfo.getRechargeDesc() != null) {
            queryWrapper.eq("recharge_desc", rechargeInfo.getRechargeDesc());
        }
        if (StringUtils.isNotEmpty(String.valueOf(rechargeInfo.getDataStatus())) && rechargeInfo.getDataStatus() != null) {
            queryWrapper.eq("data_status", rechargeInfo.getDataStatus());
        }
        if (StringUtils.isNotEmpty(String.valueOf(rechargeInfo.getCreateUser())) && rechargeInfo.getCreateUser() != null) {
            queryWrapper.eq("create_user", rechargeInfo.getCreateUser());
        }
        if (StringUtils.isNotEmpty(String.valueOf(rechargeInfo.getCreateName())) && rechargeInfo.getCreateName() != null) {
            queryWrapper.eq("create_name", rechargeInfo.getCreateName());
        }
        if (StringUtils.isNotEmpty(String.valueOf(rechargeInfo.getUpdateUser())) && rechargeInfo.getUpdateUser() != null) {
            queryWrapper.eq("update_user", rechargeInfo.getUpdateUser());
        }
        if (StringUtils.isNotEmpty(String.valueOf(rechargeInfo.getUpdateName())) && rechargeInfo.getUpdateName() != null) {
            queryWrapper.eq("update_name", rechargeInfo.getUpdateName());
        }
        queryWrapper.orderByDesc("recharge_id");
        List<RechargeInfo> rechargeInfos = rechargeInfoMapper.selectList(queryWrapper);
        if (CollectionUtil.isNotEmpty(rechargeInfos)) {
            List<CustomerLevel> levels = customerLevelService.queryList(new CustomerLevel());
            Map<Long,String> levelMap = new HashMap<>();
            for (CustomerLevel cl:levels ) {
                levelMap.put(cl.getLevelId(),cl.getLevelName());
            }
            return rechargeInfos.stream().map(recharge -> {
                if (recharge.getMemberLevel() != null) {
                    recharge.setMemberLevelName(levelMap.get(recharge.getMemberLevel()));
                }
                return recharge;
            }).collect(Collectors.toList());
        }

        return rechargeInfos;
    }

    @Override
    public Integer queryCount(RechargeInfo rechargeInfo) {
        QueryWrapper<RechargeInfo> queryWrapper = Wrappers.query();
        if (StringUtils.isNotEmpty(String.valueOf(rechargeInfo.getRechargeId())) && rechargeInfo.getRechargeId() != null) {
            queryWrapper.eq("recharge_id", rechargeInfo.getRechargeId());
        }

        if (StringUtils.isNotEmpty(String.valueOf(rechargeInfo.getRechargeName())) && rechargeInfo.getRechargeName() != null) {
            queryWrapper.like("recharge_name", rechargeInfo.getRechargeName());
        }
        if (StringUtils.isNotEmpty(String.valueOf(rechargeInfo.getIsRecommend())) && rechargeInfo.getIsRecommend() != null) {
            queryWrapper.eq("is_recommend", rechargeInfo.getIsRecommend());
        }
        if (StringUtils.isNotEmpty(String.valueOf(rechargeInfo.getRechargeIcon())) && rechargeInfo.getRechargeIcon() != null) {
            queryWrapper.eq("recharge_icon", rechargeInfo.getRechargeIcon());
        }
        if (StringUtils.isNotEmpty(String.valueOf(rechargeInfo.getRechargeSort())) && rechargeInfo.getRechargeSort() != null) {
            queryWrapper.eq("recharge_sort", rechargeInfo.getRechargeSort());
        }
        queryWrapper.ne("data_status", DataStatus.delete.getKey());
        if (StringUtils.isNotEmpty(String.valueOf(rechargeInfo.getStartTime())) && rechargeInfo.getStartTime() != null) {
            queryWrapper.eq("start_time", rechargeInfo.getStartTime());
        }
        if (StringUtils.isNotEmpty(String.valueOf(rechargeInfo.getEndTime())) && rechargeInfo.getEndTime() != null) {
            queryWrapper.eq("end_time", rechargeInfo.getEndTime());
        }
        if (StringUtils.isNotEmpty(String.valueOf(rechargeInfo.getRechargeMoney())) && rechargeInfo.getRechargeMoney() != null) {
            queryWrapper.eq("recharge_money", rechargeInfo.getRechargeMoney());
        }
        if (StringUtils.isNotEmpty(String.valueOf(rechargeInfo.getFirstDouble())) && rechargeInfo.getFirstDouble() != null) {
            queryWrapper.eq("first_double", rechargeInfo.getFirstDouble());
        }
        if (StringUtils.isNotEmpty(String.valueOf(rechargeInfo.getFirstDoubleMonth())) && rechargeInfo.getFirstDoubleMonth() != null) {
            queryWrapper.eq("first_double_month", rechargeInfo.getFirstDoubleMonth());
        }
        if (StringUtils.isNotEmpty(String.valueOf(rechargeInfo.getGoldCoin())) && rechargeInfo.getGoldCoin() != null) {
            queryWrapper.eq("gold_coin", rechargeInfo.getGoldCoin());
        }
        if (StringUtils.isNotEmpty(String.valueOf(rechargeInfo.getSilverCoin())) && rechargeInfo.getSilverCoin() != null) {
            queryWrapper.eq("silver_coin", rechargeInfo.getSilverCoin());
        }
        if (StringUtils.isNotEmpty(String.valueOf(rechargeInfo.getScore())) && rechargeInfo.getScore() != null) {
            queryWrapper.eq("score", rechargeInfo.getScore());
        }
        if (StringUtils.isNotEmpty(String.valueOf(rechargeInfo.getRechargeDesc())) && rechargeInfo.getRechargeDesc() != null) {
            queryWrapper.eq("recharge_desc", rechargeInfo.getRechargeDesc());
        }
        if (StringUtils.isNotEmpty(String.valueOf(rechargeInfo.getDataStatus())) && rechargeInfo.getDataStatus() != null) {
            queryWrapper.eq("data_status", rechargeInfo.getDataStatus());
        }
        if (StringUtils.isNotEmpty(String.valueOf(rechargeInfo.getCreateUser())) && rechargeInfo.getCreateUser() != null) {
            queryWrapper.eq("create_user", rechargeInfo.getCreateUser());
        }
        if (StringUtils.isNotEmpty(String.valueOf(rechargeInfo.getCreateName())) && rechargeInfo.getCreateName() != null) {
            queryWrapper.eq("create_name", rechargeInfo.getCreateName());
        }
        if (StringUtils.isNotEmpty(String.valueOf(rechargeInfo.getUpdateUser())) && rechargeInfo.getUpdateUser() != null) {
            queryWrapper.eq("update_user", rechargeInfo.getUpdateUser());
        }
        if (StringUtils.isNotEmpty(String.valueOf(rechargeInfo.getUpdateName())) && rechargeInfo.getUpdateName() != null) {
            queryWrapper.eq("update_name", rechargeInfo.getUpdateName());
        }
        queryWrapper.orderByDesc("recharge_id");
        return rechargeInfoMapper.selectCount(queryWrapper);
    }

    /**
     * 新增储值信息
     *
     * @param rechargeInfo 储值信息
     * @return 结果
     */
    @Override
    public boolean insertRechargeInfo(RechargeInfo rechargeInfo) {
        rechargeInfo.setDataStatus(DataStatus.valid.getKey());
        rechargeInfo.setCreateName(SecurityUtils.getUsername());
        rechargeInfo.setCreateUser(SecurityUtils.getUserId());
        rechargeInfo.setCreateTime(DateUtil.date());
        return this.save(rechargeInfo);
    }

    /**
     * 修改储值信息
     *
     * @param rechargeInfo 储值信息
     * @return 结果
     */
    @Override
    public boolean updateRechargeInfo(RechargeInfo rechargeInfo) {
        rechargeInfo.setUpdateName(SecurityUtils.getUsername());
        rechargeInfo.setUpdateUser(SecurityUtils.getUserId());
        rechargeInfo.setUpdateTime(DateUtil.date());
        return this.updateById(rechargeInfo);
    }

    /**
     * 批量删除储值信息
     *
     * @param rechargeIds 需要删除的储值信息ID
     * @return 结果
     */
    @Override
    public boolean deleteRechargeInfoByIds(Long[] rechargeIds) {
        UpdateWrapper<RechargeInfo> updateWrapper = new UpdateWrapper<>();
        updateWrapper.in("recharge_id", Arrays.asList(rechargeIds));
        updateWrapper.set("DATA_STATUS", DataStatus.delete.getKey());
        updateWrapper.set("UPDATE_User", SecurityUtils.getUserId());
        updateWrapper.set("UPDATE_NAME", SecurityUtils.getUsername());
        updateWrapper.set("UPDATE_TIME", DateUtil.date());
        return this.update(updateWrapper);
    }

    @Override
    public void recharge(RechargeRecord record) {
        RechargeInfo info = this.getById(record.getRechargeId());
        if (info == null || !info.getDataStatus().equals(DataStatus.valid.getKey())) {
            throw new BaseException("没有找到充值项");
        }
        PlatformStore store = storeService.getById(record.getStoreId());
        if (store == null || !store.getDataStatus().equals(DataStatus.valid.getKey())) {
            throw new BaseException("门店信息不存在");
        }

    }


}
